package org.grow.present.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.grow.present.domain.Bankcard;
import org.grow.present.dto.Msg;
import org.grow.present.interceptor.AuthInterceptor;
import org.grow.present.service.BankcardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/bankCard")
public class BankcardController {

    @Autowired
    private BankcardService bankcardService;

    @PostMapping("/saveCurrentBankCard")
    public Bankcard saveCurrentBankCard(@RequestBody Bankcard bankcard) {
        Integer uid = AuthInterceptor.getUID();
        QueryWrapper<Bankcard> w = new QueryWrapper<>();
        w.eq("comId", uid);
        Bankcard db = bankcardService.getOne(w);
        if (db == null) {
            bankcard.setId(0);
            bankcard.setComId(uid);
            bankcardService.save(bankcard);
        } else {
            db.setBankCardNo(bankcard.getBankCardNo());
            db.setBankName(bankcard.getBankName());
            bankcardService.updateById(db);
        }
        Bankcard db2 = bankcardService.getOne(w);
        return db2;

    }

    @GetMapping("/getCurrent")
    public Msg<Bankcard> getCurrent() {
        Integer uid = AuthInterceptor.getUID();
        QueryWrapper<Bankcard> w = new QueryWrapper<>();
        w.eq("comId", uid);
        Bankcard db = bankcardService.getOne(w);
        if(db==null){
            return Msg.fail().mess("目标不存在");
        }
        return Msg.success().data(db);
    }
}
